//
//  DataBoss.h
//  数据请求封装
//
//  Created by ju on 14-7-10.
//  Copyright (c) 2014年 scics. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "AssembleDelegate.h"
#import "ASIHTTPRequest.h"
#import "ASINetworkQueue.h"



@class DataBoss;
@class ASIFormDataRequest;
@protocol DataBossDelegate <NSObject>

- (void)fetcher:(DataBoss *)fetcher
    fetchResult:(NSArray *)objects
          error:(NSString *)errorMsg
  authorization:(BOOL)authorization
        hasMore:(BOOL)hasMore;

@end


@interface DataBoss : NSObject<ASIHTTPRequestDelegate,AssembleDelegate>


/**
 *  初始化DataBoss类
 *
 *  @param action 数据链接
 *
 *  @return DataBoss对象
 */
- (id)initWithAction:(NSString *)action;

/**
 *  异步请求数据，请求完成后执行completion，
 *
 *  @param completion 请求完成的回调方法,详见|jCompletion|
 */
- (void)fetchObjectsInBackground:(jCompletion)completion;

/**
 *  异步请求数据
 */
- (void)fetchObjectsInBackground;

/**
 *  刷新数据，请求第一页数据
 */
- (void)refresh;

/**
 *  下一页数据，如果禁用enablePaging，jCompletion的objects结果为空
 */
- (void)nextPage;

/**
 *  取消请求
 */
- (void)cancel;

/**
 *  发起异步请求,回调函数中返回NSDictionary
 *
 *  @param url     请求链接
 *  @param queue   管理请求的队列,详情查看 |ASINetworkQueue|
 *  @param handler 异步请求的回调
 */
+ (void) sendAsynchronousRequest:(NSString *)url queue:(ASINetworkQueue*)queue completionHandler:(void (^)(ASIHTTPRequest *request, NSDictionary *json, NSString *errorMsg))handler;

+ (void) sendAsynchronousRequest:(NSString *)url
                         headers:(NSDictionary *)headers
                           queue:(ASINetworkQueue*)queue
               completionHandler:(void (^)(ASIHTTPRequest *request, NSDictionary *json, NSString *errorMsg))handler;


/**
 *  发起异步请求,回调函数中返回NSData
 *
 *  @param url         请求链接
 *  @param cachePolicy 缓存策略,查看 |ASICachePolicy|
 *  @param queue       管理请求的队列,详情查看 |ASINetworkQueue|
 *  @param handler     异步请求的回调
 */
+ (void)sendAsynchronousRequest:(NSString *)url cachePolicy:(ASICachePolicy)cachePolicy queue:(ASINetworkQueue *)queue completionHandlerWithData:(void (^)(ASIHTTPRequest *request, NSData *data, NSString *errorMsg))handler;

/**
 *  发起异步请求,回调函数中返回NSDictionary
 *
 *  @param url         请求链接
 *  @param cachePolicy 缓存策略,查看 |ASICachePolicy|
 *  @param queue       管理请求的队列,详情查看 |ASINetworkQueue|
 *  @param handler     异步请求的回调
 */
+ (void)sendAsynchronousRequest:(NSString *)url cachePolicy:(ASICachePolicy)cachePolicy queue:(ASINetworkQueue *)queue completionHandlerWithDictionary:(void (^)(ASIHTTPRequest *requset, NSDictionary *json, NSString *errorMsg))handler;

/**
 *  请求并且缓存图片，回调函数返回UIImage对象
 *
 *  @param picturePath 图片路径
 *  @param _queue      请求队列，记得在dealloc的时候取消queue里面的请求
 *  @param handler     回调函数
 */
+ (ASIHTTPRequest *)requsetAndCacheImage:(NSString *)picturePath queue:(ASINetworkQueue *)queue
                  completion:(void(^)(ASIHTTPRequest *request , UIImage *image , NSString *errorMsg))handler;

/**
 *  延时请求并且缓存，回调函数返回UIImage，调用[queue go]开始请求图片
 *
 *  @param picturePath 图片路径
 *  @param queue       请求队列，记得在dealloc的时候取消queue里面的请求
 *  @param handler     回调函数
 */
+ (void)requsetAndCacheImageLazy:(NSString *)picturePath queue:(ASINetworkQueue *)queue
                      completion:(void(^)(ASIHTTPRequest *request , UIImage *image , NSString *errorMsg))handler;

/**
 *  请求并且缓存文件，返回缓存路径，通过缓存路径取得文件
 *
 *  @param fileURL 文件路径
 *  @param queue   请求队列
 *  @param handler 回调函数
 */
+ (void)requestAndCacheFile:(NSString *)fileURL queue:(ASINetworkQueue *)queue
                 completion:(void(^)(ASIHTTPRequest *request, NSString* cachedPath, NSString* errorMsg))handler;

/**
 *  上传文件到服务器，可以携带其他参数
 *
 *  @param data      文件的NSData
 *  @param server    上传链接
 *  @param key       服务器接受文件的key
 *  @param type      文件类型
 *  @param queue     上传队列
 *  @param params    其他参数
 *  @param handler   上传成功的回调block
 */
+ (void)uploadFile:(NSData *)data
          toServer:(NSString *)server
           withKey:(NSString *)key
       contentType:(NSString *)type
             queue:(ASINetworkQueue *)queue
         andParams:(NSDictionary *)params
          complete:(void(^)(ASIFormDataRequest *request,NSDictionary *json,NSString *errorMsg))handler;

/**
 *  批量上传文件到服务器，可携带
 *
 *  @param datas   文件的NSData列表
 *  @param server  服务器
 *  @param key     服务器接受文件的关键字(key)
 *  @param type    文件类型
 *  @param queue   上传队列
 *  @param params  其它参数
 *  @param handler 回调方法
 */
+ (void)uploadFiles:(NSArray *)datas
           toServer:(NSString *)server
            withKey:(NSString *)key
        contentType:(NSString *)type
              queue:(ASINetworkQueue *)queue
          andParams:(NSDictionary *)params
           complete:(void (^)(ASIFormDataRequest *, NSDictionary *, NSString *))handler;


/**
 *  post数据到服务器，回调方法返回JSON数据
 *
 *  @param params  参数列表
 *  @param server  服务器地址
 *  @param queue   队列
 *  @param handler 回调方法
 */
+ (void)postParams:(NSDictionary *)params
          toServer:(NSString *)server
             queue:(ASINetworkQueue *)queue
          complete:(void(^)(ASIFormDataRequest *request,NSDictionary *json,NSString *errorMsg))handler;

+ (void)postParams:(NSDictionary *)params
           headers:(NSDictionary *)headers
          toServer:(NSString *)server
             queue:(ASINetworkQueue *)queue
          complete:(void(^)(ASIFormDataRequest *request,NSDictionary *json,NSString *errorMsg))handler;

+ (void)putParams:(NSDictionary *)params
          headers:(NSDictionary *)headers
         toServer:(NSString *)server
            queue:(ASINetworkQueue *)queue
         complete:(void(^)(ASIFormDataRequest *request,NSDictionary *json,NSString *errorMsg))handler;

/**
 *  批量上传文件，根据文件名字和文件路径前缀
 *
 *  @param filenames 文件名列表
 *  @param prefix    文件路径前缀
 *  @param server    服务器地址
 *  @param key       服务器接受文件的关键字(key)
 *  @param type      文件类型
 *  @param queue     请求队列
 *  @param params    其它参数
 *  @param handler   回调方法
 */
+ (void)uploadFilesByName:(NSArray *)filenames
                   prefix:(NSString*)prefix
                 toServer:(NSString *)server
                  withKey:(NSString *)key
              contentType:(NSString *)type
                    queue:(ASINetworkQueue *)queue
                andParams:(NSDictionary *)params
                 complete:(void (^)(ASIFormDataRequest *requset, NSDictionary *json, NSString *errorMsg))handler;


+ (void)sendAsynchronousRequestWithPost:(NSString *)url
                                  queue:(ASINetworkQueue *)queue
                              postValue:(NSDictionary *)postValue
        completionHandlerWithDictionary:(void (^)(ASIHTTPRequest *request, NSDictionary *json, NSString *errorMsg))handler;


@property (nonatomic) BOOL enablePaging;
@property (nonatomic) NSInteger timeout;
@property (nonatomic) NSInteger limit;
@property (nonatomic) NSInteger page;
@property (nonatomic,strong) NSString *action;
@property (nonatomic,strong) jCompletion completion;
@property (nonatomic,weak) id<DataBossDelegate> delegate;

@end




